const baseUrl = 'http://192.168.1.181:8081';
// const baseUrl = 'https://831y8t3126.goho.co'; //手机测试

export default function request({
	url,
	method,
	data,
	header = {},
	ignoreToken = false
}) {
	return new Promise((resolve, reject) => {
		let token = localStorage.getItem('token');
		if (token !== undefined && token !== null && token !== '') {
			header['Authorization'] = token;
		}
		if (!ignoreToken && !token) {
			uni.navigateTo({
				url: '/pages/login/login'
			});
			reject('未登录，跳转登录');
		}
		uni.request({
			url: baseUrl + url,
			method: method,
			data: data,
			header: header,
			success: (res) => {
				if (res && res.data && res.data.code) {
					if (res.data.code === 200) {
						resolve(res.data);
					} else if (res.data.code === 401) {
						localStorage.removeItem('token')
						uni.navigateTo({
							url: '/pages/login/login'
						});
						uni.showToast({
							title: '登录过期，跳转登录',
							icon: 'none'
						});
						reject('登录过期，跳转登录');
					} else if (res.data.code === 403) {
						uni.showToast({
							title: '没有权限',
							icon: 'none'
						});
						reject('没有权限');
					} else {
						uni.showToast({
							title: res.data.msg || '服务器错误',
							icon: 'none'
						});
						reject(res.data.msg || '服务器错误');
					}
				} else {
					uni.showToast({
						title: '服务器响应错误',
						icon: 'none'
					});
					reject('服务器响应错误');
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '请求失败，请检查网络状态',
					icon: 'error'
				});
			},
		});
	});
}